#include <iostream>
#include <signal.h>
#include <unistd.h>
using namespace std;

void handler(int sigid)
{
    cout << "Catch a signal:" << sigid << endl;
}
void printPending(sigset_t &pending)
{
    for (int i = 31; i >= 1; --i)
    {
        if (sigismember(&pending, i))
        {
            cout << 1;
        }
        else
        {
            cout << 0;
        }
    }
    cout << endl;
}
int main()
{
    signal(2, handler);
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, 2);
    sigprocmask(SIG_BLOCK, &set, nullptr);

    sigset_t pending;
    int cnt = 0;
    while (1)
    {
        int ret = sigpending(&pending);
        if (ret < 0)
            continue;
        printPending(pending);
        sleep(1);
        ++cnt;
        if (cnt == 20)
        {
            cout << "unblock 2 sigNo" << endl;
            sigprocmask(SIG_UNBLOCK, &set, nullptr);
        }
    }

    return 0;
}